package cn.itrip.controller;

import cn.itrip.DTO.ItripHotelRoomDTO;
import cn.itrip.DTO.ItripImageDTO;
import cn.itrip.DTO.ItripLabelDicDTO;
import cn.itrip.entity.ItripImage;
import cn.itrip.entity.ItripLabelDic;
import cn.itrip.service.ItripHotelRoomService;
import cn.itrip.service.ItripImageService;
import cn.itrip.service.ItripLabelDicService;
import cn.itrip.utils.DateUtil;
import cn.itrip.utils.EmptyUtils;
import cn.itrip.vo.R;
import cn.itrip.vo.biz.SearchHotelRoomVO;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

@RestController
@RequestMapping("/api/hotelroom")
public class HotelRoomController {

    @Resource
    private ItripImageService itripImageService;
    @Resource
    private ItripLabelDicService itripLabelDicService;
    @Resource
    private ItripHotelRoomService itripHotelRoomService;

    @ApiOperation(value = "查询酒店房型的图片")
    @ApiResponses({ @ApiResponse(code = 200, message = "OK",response = ItripImageDTO.class) })
    @GetMapping("/getimg/{roomId}")
    public R getimg(@PathVariable("roomId") String roomId){
        List<ItripImage> list = itripImageService.getListByRoomId(roomId,"1");
        List<ItripImageDTO> itripImageDTOS = new ArrayList<>();
        for (int i = 0;i<list.size();i++){
                ItripImageDTO itripImageDTO = new ItripImageDTO();
                itripImageDTO.setImgUrl(list.get(i).getImgUrl());
                itripImageDTO.setPosition(list.get(i).getPosition());
                itripImageDTOS.add(itripImageDTO);
            }
        return R.success("获取酒店图片房型成功",itripImageDTOS);
    }

    @ApiOperation(value = "查询酒店房型列表")
    @ApiResponses({ @ApiResponse(code = 200, message = "OK") })
    @PostMapping("/queryhotelroombyhotel")
    public R queryhotelroombyhotel(@RequestBody SearchHotelRoomVO vo ){
        List<List<ItripHotelRoomDTO>> hotelRoomDTOList = null;
        try {
            if (EmptyUtils.isEmpty(vo.getHotelId())) {
                R r = new R();
                r.setErrorCode("100303");
                r.setMsg("酒店ID不能为空");
                return r;
            }
            if (EmptyUtils.isEmpty(vo.getStartDate()) || EmptyUtils.isEmpty(vo.getEndDate())) {
                R r = new R();
                r.setErrorCode("100303");
                r.setMsg("必须填写酒店入住及退房时间");
                return r;
            }
            if (EmptyUtils.isNotEmpty(vo.getStartDate()) && EmptyUtils.isNotEmpty(vo.getEndDate())) {
                if (vo.getStartDate().getTime() > vo.getEndDate().getTime()) {
                    R r = new R();
                    r.setErrorCode("100303");
                    r.setMsg("入住时间不能大于退房时间");
                    return r;
                }
                List<Date> dates = DateUtil.getBetweenDates(vo.getStartDate(), vo.getEndDate());
            }

            vo.setIsHavingBreakfast(EmptyUtils.isEmpty(vo.getIsHavingBreakfast()) ? null : vo.getIsHavingBreakfast());
            vo.setIsBook(EmptyUtils.isEmpty(vo.getIsBook()) ? null : vo.getIsBook());
            vo.setIsTimelyResponse(EmptyUtils.isEmpty(vo.getIsTimelyResponse()) ? null : vo.getIsTimelyResponse());
            vo.setRoomBedTypeId(EmptyUtils.isEmpty(vo.getRoomBedTypeId()) ? null : vo.getRoomBedTypeId());
            vo.setIsCancel(EmptyUtils.isEmpty(vo.getIsCancel()) ? null : vo.getIsCancel());
            vo.setPayType(EmptyUtils.isEmpty(vo.getPayType()) ? null : vo.getPayType());
            List<ItripHotelRoomDTO> originalRoomList = itripHotelRoomService.getItripHotelRoomListByMap(vo);
            hotelRoomDTOList = new ArrayList();
            for (ItripHotelRoomDTO roomDTO : originalRoomList) {
                List<ItripHotelRoomDTO> tempList = new ArrayList<ItripHotelRoomDTO>();
                tempList.add(roomDTO);
                hotelRoomDTOList.add(tempList);
            }
            return R.success("获取成功", hotelRoomDTOList);
        } catch (Exception e) {
            e.printStackTrace();
            R r = new R();
            r.setErrorCode("100304");
            r.setMsg("获取酒店房型列表失败");
            return r;
        }
    }

    @ApiOperation(value = "查询所有床型")
    @ApiResponses({ @ApiResponse(code = 200, message = "OK", response = ItripLabelDicDTO.class) })
    @GetMapping("/queryhotelroombed")
    public R queryhotelroombed(){
        List<ItripLabelDic> list = itripLabelDicService.queryHotelRoombed();
        List<ItripLabelDicDTO> itripLabelDicDTOS = new ArrayList<>();
        for (int i = 0;i<list.size();i++){
            ItripLabelDicDTO itripLabelDicDTO = new ItripLabelDicDTO();
            itripLabelDicDTO.setDescription(list.get(i).getDescription());
            itripLabelDicDTO.setId(list.get(i).getId());
            itripLabelDicDTO.setName(list.get(i).getName());
            itripLabelDicDTO.setPic(list.get(i).getPic());
            itripLabelDicDTOS.add(itripLabelDicDTO);
        }
        return R.success("获取成功",itripLabelDicDTOS);
    }
}
